<?php

namespace HikCloud;

use think\facade\Cache;

class HikCloud
{
    //消息ID
    public $consumerId;
    //配置文件
    public static $config;
    //私有属性，用于保存实例
    private static $instance;
    //私有属性，用于保存实例
    public static $ApiMaps = [];

    /**
     * HikCloud constructor.
     */
    public function __construct($config = [])
    {
        if ($config !== []) {
            static::$config = $config;
        }

        // 缓存配置
        Cache::config([
            'default' => 'file',
            'stores'  => [
                'file' => [
                    'type'   => 'File',
                    // 缓存保存目录
                    'path'   => './cache/',
                    // 缓存前缀
                    'prefix' => '',
                    // 缓存有效期 0表示永久缓存
                    'expire' => 0,
                ],
            ],
        ]);

    }

    /**
     * 获取配置
     * @param null $name
     * @return mixed
     */
    public static function getConfig($name = null)
    {
        if ($name !== null) {
            return isset(static::$config[$name]) ? static::$config[$name] : false;
        }
        return static::$config;
    }

    /**
     * 配置是否存在
     * @param $name
     * @return bool
     */
    public static function ConfHash($name)
    {
        if (is_string($name)) {
            return isset(static::$config[$name]) ? true : false;
        }
        return false;
    }

    /**
     * 设置配置
     * @param null $name
     * @param null $datas
     * @return bool
     */
    public static function setConfig($name = null, $datas = null)
    {

        if (is_array($name)) {
            foreach ($name as $key => $data) {
                static::$config[$key] = $data;
            }
        }
        if (is_string($name)) {
            static::$config[$name] = $datas;
        }

        return false;
    }

    /**
     * 公有方法，用于获取实例
     */
    public static function getInstance($config = [])
    {
        if ($config !== []) {
            static::$config = $config;
        }
        //判断实例有无创建，没有的话创建实例并返回，有的话直接返回
        if (!(self::$instance instanceof self)) {
            self::$instance = new self(static::$config);
        }
        return self::$instance;
    }

    /**
     * @param $method
     * @param array|null $params
     * @return array|bool|mixed
     */
    public function request($method, array $params = null)
    {
        try {
            $accessToken = new AccessToken(static::$config);
            $token = $accessToken->getToken();
            $request = HikRequest::getInstance();
            $request->initRequest($method, $params, $token['access_token']);
            return $request->sendRequest();
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * @return array
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function getConsumerId()
    {
        self::$instance->consumerId = Cache::get('consumerId');
        if (!self::$instance->consumerId) {
            $res = self::$instance->request('Estate.EntranceGuard.MqCreate');
            if ($res['code'] == 200) {
                self::$instance->consumerId = $res['data']['consumerId'];
                var_dump("---------获取新消息ID：" . self::$instance->consumerId . "---------");
                Cache::set('consumerId', self::$instance->consumerId, 30);
                return [ 'code' => 200, 'consumerId' => self::$instance->consumerId ];
            }
            return $res;
        }
        Cache::set('consumerId', self::$instance->consumerId, 30);
        var_dump("---------获取已存在消息ID：" . self::$instance->consumerId . "---------");
        return [ 'code' => 200, 'data' => self::$instance->consumerId ];
    }
}